/**
 *
 * Copyright Pact Lab of H.I.T.
 *
 * Designed and Implemented by Grid Researching Group, 
 * Pact Lab, Harbin
 * 
 * This Project is part of the national 973 Project:
 * Internet Based Virtual Computing Environment
 *
 * http://pact518.hit.edu.cn
 * 
 * Author:       Meteor <meteorlxk@gmail.com> 
 * Copyright:    pact518 
 * Version:      1.0
 * Created:      2008-11-18 
 * LastModified: 2008-11-18
 */
package edu.hit.pact.pgse.crawler.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.hit.pact.pgse.util.Utilities;

public class FileFilter {
	private Log logger;
	
	private Set<String> supportedSuffixList;
	
	private Set<String> unsupportedSuffixList;
	
	public FileFilter(String supportedFile, String unsupportedFile) throws IOException{
		this(LogFactory.getLog(FileFilter.class), supportedFile, unsupportedFile);
	}
	
	public FileFilter(Log logger, String supportedFile, String unsupportedFile) throws IOException{
		this(logger, Utilities.getInputString(supportedFile), Utilities.getInputString(unsupportedFile));
	}
	
	public FileFilter(InputStream supportedInputStream, InputStream unsupportedInputStream) throws IOException {		
		this(LogFactory.getLog(FileFilter.class), supportedInputStream, unsupportedInputStream);
	}
	
	public FileFilter(Log logger, InputStream supportedInputStream, InputStream unsupportedInputStream) throws IOException {
		this.logger = logger;
	
		Set<String> set = Utilities.parse(supportedInputStream);
		setSupported(set);
		
		set = Utilities.parse(unsupportedInputStream);
		setUnsupported(set);
	}
	
	private void setSupported(Set<String> set) {
		supportedSuffixList = new HashSet<String>();
		for (String line : set) {
			/*
			 * url's suffix can be low case such as jpg, but also can be up case such as JPG.
			 * this way can fix all cases.
			 */
			supportedSuffixList.add(line.toLowerCase());
			supportedSuffixList.add(line.toUpperCase());
		}//end for
	}
	
	private void setUnsupported(Set<String> set) {
		unsupportedSuffixList = new HashSet<String>();
		for (String line : set) {
			/*
			 * url's suffix can be low case such as jpg, but also can be up case such as JPG.
			 * this way can fix all cases.
			 */
			unsupportedSuffixList.add(line.toLowerCase());
			unsupportedSuffixList.add(line.toUpperCase());
		}//end for
	}
	
	private boolean isSupported(String fileName)
	{
		String suffix = distillSuffix(fileName);
		if(null == suffix)
		{
			return false;
		}//end if
		return supportedSuffixList.contains(suffix);
	}
	
	private boolean isUnsupported(String fileName)
	{
		String suffix = distillSuffix(fileName);
		if(null == suffix)
		{
			return false;
		}//end if
		return unsupportedSuffixList.contains(suffix);
	}
	
	private String distillSuffix(String fileName)
	{
		int index = fileName.lastIndexOf(".");
		if(-1 == index)
		{
			return null;
		}else{
			return fileName.substring(index);
		}//end if
	}
	
	public boolean iCanProcess(String file) {
		if (isSupported(file)) {
//			logger.info("I CAN process url = " + file);
			return true;
		} else if (isUnsupported(file)) {
			logger.info("I CANNOT process url = " + file);
			return false;
		} else {
			/*
			 * if not true, urls like "http://www.sina.com.cn" (without any
			 * suffix concerning the file type) will BE skipped out
			 */
			logger.info("I CAN process url = " + file);
			return true;
		}//end if
	}
}
